package com.my.oauth2.resource.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("getCurrenUser")
    public Object getAuth(Authentication authentication, HttpServletRequest request){
        //jwt方式的解析
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(authorization.indexOf("bearer") + 7);
        Claims body = Jwts.parser().setSigningKey("key".getBytes(StandardCharsets.UTF_8)).parseClaimsJws(token)
                .getBody();
        return body;
        //redis方式直接获取
//        return authentication.getPrincipal();
    }
}
